package koscom.corebase.nam;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.table.DefaultTableModel;

import redis.clients.jedis.Jedis;

@SuppressWarnings("serial")
public class ItemSearch extends JFrame {

	private JPanel contentPane;
	private Font font;//, font2;
	DefaultTableModel itemTableModel;
	JTextField itemCodeTextField;

	int a;
	Jedis jedis;
	
	public void disappear(String itemCode)
	{
		this.dispose();
	}
	
	public ItemSearch(Jedis jj, JTextField itemCodeField) {
		jedis = jj;
		itemCodeTextField = itemCodeField;
		setTitle("CoreBASE [4009] 종목조회");
		font = new Font("맑은 고딕", Font.PLAIN, 12);
		//font2 = new Font("맑은 고딕", Font.BOLD, 12);

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		setBounds(500, 100, 408, 550);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JPanel panel = new JPanel();
		panel.setLayout(null);
		panel.setForeground(Color.BLACK);
		panel.setBorder(new LineBorder(new Color(192, 192, 192)));
		panel.setBackground(new Color(236, 236, 236));
		panel.setBounds(12, 10, 375, 74);

		design();
		contentPane.add(panel);

		btn_submit = new JButton("전송");
		btn_submit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				itemTableModel.setRowCount(0);
				String itemCode = textfields[0].getText();
				for(String key : jedis.keys("*"+itemCode+"*.종목번호"))
				{
					String name = key.split("\\.")[0];
					//System.out.println(jedis.get(key)+"\t"+name);
					String row[] = {jedis.get(key), name};
					itemTableModel.addRow(row);
				}
			}
		});
		btn_submit.setBounds(283, 23, 70, 23);
		panel.add(btn_submit);
		btn_submit.setFont(font);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setVerifyInputWhenFocusTarget(false);
		scrollPane.setEnabled(false);
		scrollPane.setBounds(12, 100, 376, 373);
		contentPane.add(scrollPane);
		
		itemTableModel = new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"종목번호", "종목명"
			}
		);
		table = new JTable(itemTableModel){
			public boolean isCellEditable(int row, int column) {
			       //all cells false
			       return false;
			    }
		};
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getClickCount() == 2) {
			         JTable target = (JTable)e.getSource();
			         int row = target.getSelectedRow();
			         itemCodeTextField.setText("");
//			         int column = target.getSelectedColumn();			         
//			         System.out.println("row : "+row);
//			         System.out.println("col : "+column);
//			         System.out.println(table.getValueAt(row, column));
//			         System.out.println(table.getValueAt(row, 0)); //always get code number\
			         String itemCode = table.getValueAt(row,0).toString();
			         
			         itemCodeTextField.setText(itemCode);
			         disappear(itemCode);
				}
				
			}
		});
		
		table.setVerifyInputWhenFocusTarget(false);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane.setViewportView(table);

	}

	JTextField[] textfields;
	JTextField[] text_output;
	JPasswordField password;
	JButton btn_search, btn_submit;
	int passibleCount = 0;
	JComboBox comboBox;
	String resultMsg = ":";
	DefaultListModel listModel;
	private JTable table;
	public void design() {

		int rowSize = 1;
		int jl_mark_top = 35;
		textfields = new JTextField[rowSize];
		text_output = new JTextField[rowSize];

		final String[] names = { "종목이름" };

		// 반복적인 것들
		for (int i = 0; i < rowSize; i++) {

			JLabel jl_mark = new JLabel(">");
			jl_mark.setBounds(30, jl_mark_top + 25 * i, 57, 15);
			if (i < rowSize) {
				jl_mark.setForeground(new Color(220, 20, 60));
			}

			contentPane.add(jl_mark);

			JLabel jl_name = new JLabel(names[i]);
			jl_name.setFont(font);
			jl_name.setBounds(50, jl_mark_top + 25 * i, 57, 15);
			contentPane.add(jl_name);

			if (i == 0) {
				textfields[i] = new JTextField();
				textfields[i].setFont(font);
				textfields[i].setColumns(10);
				textfields[i].setBounds(150, jl_mark_top + i * 25, 120, 21);
				contentPane.add(textfields[i]);
			}

			if (i == 1) {
				text_output[i] = new JTextField();
				text_output[i].setFont(font);
				text_output[i].setColumns(10);
				text_output[i].setEditable(false);
				text_output[i].setBounds(300, jl_mark_top + i * 25, 120, 21);
				contentPane.add(text_output[i]);
			}

		}
		listModel = new DefaultListModel();
		
		JLabel jlresult = new JLabel(resultMsg);
		jlresult.setBounds(12, 483, 375, 23);
		jlresult.setFont(font);
		contentPane.add(jlresult);
		
	}
}
